package libaryinterface;

import book.Login;
import jdbc.DisplayJdbc;
import jdbc.SearchJdbc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import static javax.swing.WindowConstants.DISPOSE_ON_CLOSE;

public class AdminInterface {
        JFrame f1;
        JPanel p1;
        JButton bAdd, bDelete, bSearch, bShow,breturn;

    public AdminInterface() {
        f1 = new JFrame("管理员界面");
        p1 = new JPanel();
        bAdd = new JButton("添加书籍");
        bDelete = new JButton("删除书籍");
        bSearch = new JButton("查找书籍");
        bShow = new JButton("显示/修改书籍");
        breturn = new JButton("返回登录界面");

        p1.setLayout(null);
        bAdd.setBounds(120,40,250,50);
        bDelete.setBounds(120,120,250,50);
        bSearch.setBounds(120,200,250,50);
        bShow.setBounds(120,280,250,50);
        breturn.setBounds(120,360,250,50);

        f1.add(p1);
        p1.add(bAdd);
        p1.add(bSearch);
        p1.add(bShow);
        p1.add(bDelete);
        p1.add(breturn);
        f1.setVisible(true);
        f1.setBounds(500,150,500,500);

        //添加书籍按钮监听器
        bAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new AddInterface();
            }
        });
        //展示书籍按钮监听器
        bShow.addActionListener(new ActionListener() {
            ResultSet result;
            public void actionPerformed(ActionEvent e) {
                try {
                    DisplayJdbc displayJdbc = new DisplayJdbc();
                    result = displayJdbc.Display ();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                //弹窗
                try {
                    if( !result.next() ){
                        new MyDialogDemo("书库为空!");
                    } else {
                        try {
                            new DisplayInterface(result);
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }

            }
        });
        //删除书籍按钮监听器
        bDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new DeleteInterface();
                } catch (FontFormatException ex) {
                    ex.printStackTrace();
                }
            }
        });
        //查找书籍按钮监听器
        bSearch.addActionListener(new ActionListener() {
            //ResultSet result;
            public void actionPerformed(ActionEvent e) {
               new SearchInterface();
            }
        });

        //返回按钮监听
        breturn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login();
                f1.dispose();;
            }
        });
        f1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

}
